package com.zjhd.sealcutting.service.impl;

import com.google.common.collect.Maps;
import com.zjhd.sealcutting.dao.ExpressInfoMapper;
import com.zjhd.sealcutting.dao.ExpressPictureMapper;
import com.zjhd.sealcutting.dao.UserInfoMapper;
import com.zjhd.sealcutting.domain.ExpressInfo;
import com.zjhd.sealcutting.domain.ExpressPicture;
import com.zjhd.sealcutting.domain.UserInfo;
import com.zjhd.sealcutting.param.ExpressSearchQuery;
import com.zjhd.sealcutting.service.ExpressInfoService;
import com.zjhd.sealcutting.utils.DateUtil;
import com.zjhd.sealcutting.utils.response.ResponseResult;
import com.zjhd.sealcutting.utils.response.ResultGenerator;
import com.zjhd.sealcutting.vo.ExpressInfoVo;
import com.zjhd.sealcutting.vo.SearchResult;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * TODO  快递管理
 *
 * @author wangchao
 * @version 1.0
 * @date 2020/11/25 17:41
 */
@Service
public class ExpressInfoServiceImpl implements ExpressInfoService {

    @Autowired
    private ExpressInfoMapper expressInfoMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private ExpressPictureMapper expressPictureMapper;

    @Override
    public SearchResult queryExpressParamlist(ExpressSearchQuery param) throws Exception {
        //定义map 封装查询参数到map中
        Map<String, Object> map = Maps.newLinkedHashMap();
        if(StringUtils.isNotBlank(param.getCourierNumber())){
            map.put("courierNumber",param.getCourierNumber());
        }
        //当前页  分页数
        map.put("pageIndex", (Integer.parseInt(param.getPageIndex()) - 1) * Integer.parseInt(param.getPageSize()));
        map.put("pageSize", Integer.parseInt(param.getPageSize()));
        //查询用户信息
        UserInfo userInfo=userInfoMapper.selectByPrimaryKey(param.getUserId());
        if(userInfo!=null){
            if(userInfo.getUserGroup().equals("2")){
                map.put("createrId", userInfo.getId());
            }
            if(userInfo.getUserGroup().equals("5")){
                map.put("receivPlaceName", userInfo.getCompany());
            }
        }
        List<ExpressInfo> expressInfos=expressInfoMapper.queryParamExpressList(map);
        long totalCount=expressInfoMapper.queryParamExpressCount(map);
        if(expressInfos.size()>0){
            return new SearchResult.Builder<List<ExpressInfo>>()
                    .setCurrent(Integer.parseInt(param.getPageIndex()))
                    .setPageSize(Integer.parseInt(param.getPageSize()))
                    .setTotalCount(totalCount)
                    .setPagedItems(expressInfos == null ? new ArrayList() : expressInfos)
                    .build();
        }else {
            return new SearchResult.Builder().setPagedItems(new ArrayList()).build();
        }
    }

    /**
     * TODO  新增快递
     * @author wangchao
     * @date 2020/11/25 17:56
     * @param
     * @return
     */
    @Override
    public ResponseResult insertExpressInfo(ExpressInfoVo expressInfoVo) throws Exception {
        try {
            expressInfoVo.getExpressInfo().setState(0);
            expressInfoVo.getExpressInfo().setCreateTime(DateUtil.getCurrentTime());
            int i=expressInfoMapper.insert(expressInfoVo.getExpressInfo());
            for(ExpressPicture expressPicture:expressInfoVo.getPictureList()){
                expressPicture.setExpressId(expressInfoVo.getExpressInfo().getId());
                expressPictureMapper.insert(expressPicture);
            }
            if(i>0){
                return ResultGenerator.genSuccessResult("新增快递成功!");
            }else {
                return ResultGenerator.genFailResult("新增快递失败!");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultGenerator.genFailResult("新增快递失败!");
    }

    /**
     * TODO 修改快递
     * @author wangchao
     * @date 2020/11/25 17:56
     * @param
     * @return
     */
    @Override
    public ResponseResult updateExpressInfo(ExpressInfoVo expressInfoVo) throws Exception {
        try {
            int i=expressInfoMapper.updateByPrimaryKeySelective(expressInfoVo.getExpressInfo());
            expressPictureMapper.deleteByExpressId(expressInfoVo.getExpressInfo().getId());
            for(ExpressPicture expressPicture:expressInfoVo.getPictureList()){
                expressPicture.setExpressId(expressInfoVo.getExpressInfo().getId());
                expressPictureMapper.insert(expressPicture);
            }
            if(i>0){
                return ResultGenerator.genSuccessResult("修改快递成功!");
            }else {
                return ResultGenerator.genFailResult("修改快递失败!");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultGenerator.genFailResult("新增快递失败!");
    }

    /**
     * TODO  删除快递信息
     * @author wangchao
     * @date 2020/11/25 18:00
     * @param
     * @return
     */
    @Override
    public ResponseResult deleteExpressInfo(Integer id) throws Exception {
        try {
            int i=expressInfoMapper.deleteByPrimaryKey(id);
            expressPictureMapper.deleteByExpressId(id);
            if(i>0){
                return ResultGenerator.genSuccessResult("删除快递成功!");
            }else {
                return ResultGenerator.genFailResult("删除快递失败!");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultGenerator.genFailResult("删除快递失败!");
    }

    /**
     * TODO  根据ID查询快递详情
     * @author wangchao
     * @date 2020/11/27 14:28
     * @param
     * @return
     */
    @Override
    public ExpressInfoVo queryByIdExpressInfo(Integer id) throws Exception {
        ExpressInfoVo expressInfoVo=new ExpressInfoVo();
        expressInfoVo.setExpressInfo(expressInfoMapper.selectByPrimaryKey(id));
        expressInfoVo.setPictureList(expressPictureMapper.queryByExpressId(id));
        return expressInfoVo;
    }
}
